<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'lishi.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <table border="2px">
   <caption><h2>历史类</h2></caption><br>
		<tr ><td>序号</td>
			<td width="250">书名</td>
			<td width="150">作者</td>
			<td width="120">书价（元）</td>
			<td width="200">出版日期</td>
		</tr>
    <%
    Connection conn=null;
	Statement stmt=null;
	ResultSet rs=null;
  	String url= "jdbc:mysql://localhost:3306/tsgl";
	String users = "root";
	String pass = "x5";  
	
	int pagesize=10;  
    int pagecount;  
    int curpage=1; 
    
    try{
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection(url,users,pass);
		stmt = conn.createStatement();
		rs=stmt.executeQuery("select count(*) as sum from lishi");
		int count=0;
		if(rs.next()){	
			count=rs.getInt("sum");
		}
		rs.close();
		pagecount=(count%pagesize==0)?(count/pagesize):(count/pagesize+1);
		String tmp=request.getParameter("curpage");  
		
		if(tmp==null){  
            tmp="1";  
        }  
        curpage=Integer.parseInt(tmp);
        rs = stmt.executeQuery("select * from lishi limit "
      								+(curpage-1)*pagesize+","+pagesize);
        
    	while(rs.next()){
    	%>
    		<tr><td><% out.print(rs.getInt(1)); %></td>
    		<td><% out.print(rs.getString(2)); %></td>
    		<td><% out.print(rs.getString(3)); %></td>
    		<td><% out.print(rs.getDouble(4)); %></td>
    		<td><% out.print(rs.getString(5)); %></td></tr><%
    	}
    	
	}finally{
		if(rs!=null){
			rs.close();
		}
		if(stmt!=null){
			stmt.close();
		}
		if(conn!=null){
			conn.close();
		}
	}		
     %>
   <br>
    </table>
     
    </table></center><br>
	<center><table class="page">
	<tr>
	
		<td><a href="wenxue.jsp?curpage=1" >首页</a></td>
		<td><a href="wenxue.jsp?curpage=<%=curpage-1%>" >上一页</a></td>
		<td><a href="wenxue.jsp?curpage=1">1</a></td>
		<td><a href="wenxue.jsp?curpage=2">2</a></td>
		<td><a href="wenxue.jsp?curpage=3">3</a></td>
		<td><a href="wenxue.jsp?curpage=4">4</a></td>
		<td><a href="wenxue.jsp?curpage=<%=pagecount%>">5</a></td>
		<td><a href="wenxue.jsp?curpage=<%=curpage+1%>">下一页</a></td>
		<td><a href="wenxue.jsp?curpage=<%=pagecount%>">尾页</a></td>
		<td><a href="mulu.jsp">导航</a></td>	
	</tr>
	</table></center>

<style type="text/css">
table.tab{	
	color:#fff;
	align:center;	
	}
.tab tr{
	height:30px; 
	text-align:center;
}
table.page{	
	color:#fff;
	align:center;	
	}
.page tr{
	height:30px; 
	text-align:center;
}
.page tr td{
	width:60px;
	text-align:center;
}</style>
     
  </body>
</html>
